'''
Created on 2009-10-25

@author: summit
'''

import wx
import os
import  wx.lib.filebrowsebutton as filebrowse
import  wx.gizmos   as  gizmos
from jolly.datamanagement.vtkPythonMetaImageData  import *
from jolly.wxext.AbstractModel import *


try:
    from agw import foldpanelbar as fpb
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.foldpanelbar as fpb

try:
    from agw import aquabutton as AB
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.aquabutton as AB


class ImportModel(AbstractModel):
    def __init__(self, collection=None, datamanager=None):
        
        AbstractModel.__init__(self)
        
        self._collection = collection
        self._datamanager = datamanager
        self._path = None
        self._files = None
    
    def SetPath(self, arg):
        self._path = arg
        
    
    def GetPath(self, arg):
        return self._path
    
    def SetFiles(self, arg):
        self._files = arg
        
    
    def GetFiles(self, arg):
        return self._files
    
    def Update(self):
        if self._path<>None:
            image = vtkPythonMetaImageData()
            image.ReadSeriesFile(self._path)
            imagedata = image.getDataSet()
            flip = vtk.vtkImageFlip()
            
#            if imagedata.GetNumberOfScalarComponents()>1:
#                il = vtk.vtkImageLuminance()
#                il.SetInput(imagedata)
#                flip.SetInput(il.GetOutput())
#                il.Update()
#            else:
#                flip.SetInput(imagedata)
#            flip.SetFilteredAxis(2)
#            flip.Update()
#            image.setDataSet(flip.GetOutput())

            if imagedata.GetNumberOfScalarComponents()>1:
                il = vtk.vtkImageLuminance()
                il.SetInput(imagedata)
                il.Update()
                image.setDataSet(il.GetOutput())
            else:
                image.setDataSet(imagedata)
                
            
            self._datamanager.AddMetaDataSet(image)
            if self._collection:
                self._collection.SyncSetInput(image.GetImageData())
                self._collection.InstallCrossAxes()
                self._collection.SyncReset()
                self._collection.SyncRender()
            self.update()

    
    


class ImportDirPanel(wx.Panel):
    '''
    
    '''
    
    def __init__(self, parent, id, model, pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.NO_BORDER | wx.TAB_TRAVERSAL):
        '''
        '''
        wx.Panel.__init__(self, parent, id, pos, size, style)
        
        self.model = model
        self.CreateControls()
        self.UpdateControl()
        self.GetSizer().Fit(self)
        self.GetSizer().SetSizeHints(self)
        self.GetSizer().Layout()
    
        self.Bind(fpb.EVT_CAPTIONBAR, self.OnPressCaption)
        
    
    def OnPressCaption(self, event):
        event.Skip()
    
    def CreateControls(self):
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        
        subpanel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
                            wx.NO_BORDER | wx.TAB_TRAVERSAL)
        sizer.Add(subpanel, 1, wx.GROW | wx.ADJUST_MINSIZE, 5)
        
        
        
        subsizer = wx.GridBagSizer(5, 5)
        subpanel.SetSizer(subsizer)
        
        # row 1
        self.dbb = filebrowse.DirBrowseButton(
            subpanel, -1, changeCallback = self.OnChangedDir
            )
       
        subsizer.Add(self.dbb, (0,0), (1,4),wx.EXPAND)
        
        # row
        self.elb = gizmos.EditableListBox(
                    subpanel, -1, "List of Files"
                    )
        subsizer.Add(self.elb, (1,0), (1,4),wx.EXPAND)
        
        # row
        self.btn = AB.AquaButton(subpanel, -1, None, "Import !")
        self.btn.SetHoverColour((0.8900*255, 0.5100*255, 0.0900*255, 255))
        self.btn.SetBackgroundColour((0.0000*255, 0.7490*255, 1.0000*255, 255))
        self.btn.SetForegroundColour((0,0,0,255))
        subsizer.Add(self.btn, (2,0), (1,4),wx.EXPAND)
        self.Bind(wx.EVT_BUTTON, self.OnImport, self.btn)
        
        subsizer.AddGrowableCol(3)
        subsizer.AddGrowableRow(3)
    
    def UpdateControl(self):
        pass
    
    def OnChangedDir(self, event):
        path = event.GetString()
        files = []
        for f in os.listdir(path):
            if os.path.isfile(os.path.join(path, f)):
                files.append(f)
        files.sort()
        self.elb.SetStrings(files)
        if self.model:
            self.model.SetPath(path)
            self.model.SetFiles(files)
    
    def OnImport(self, event):
        if self.model:
            self.model.Update()
    
if __name__=="__main__":
    class Collapsed(wx.Frame):
        def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
                 size=(400,300), style=wx.DEFAULT_FRAME_STYLE):

                 wx.Frame.__init__(self, parent, id, title, pos, size, style)
                 
                 self.CreateFoldBar()
                 
        def CreateFoldBar(self, vertical=True):
            if vertical:
                self.SetSize((500,600))
            else:
                self.SetSize((700,300))
            newstyle = (vertical and [fpb.FPB_VERTICAL] or [fpb.FPB_HORIZONTAL])[0]
            bar = fpb.FoldPanelBar(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
                           fpb.FPB_DEFAULT_STYLE | newstyle, fpb.FPB_COLLAPSE_TO_BOTTOM)
            item = bar.AddFoldPanel("Edit Import Panel", collapsed=False)
            testpanel = ImportDirPanel(item, wx.ID_ANY, None)
            bar.AddFoldPanelWindow(item, testpanel, fpb.FPB_ALIGN_WIDTH, 0, 20)
            
            if hasattr(self, "pnl"):
                self.pnl.Destroy()

            self.pnl = bar
    
            size = self.GetClientSize()
            self.pnl.SetDimensions(0, 0, size.GetWidth(), size.GetHeight())
            
    
    app = wx.PySimpleApp()
    frame = Collapsed(None)
    frame.Show(True)
    app.MainLoop()
        